package com.tanhua.dubbo.api.impl;

import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.InformApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class InformApiImpl implements InformApi {


    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 根据id分页查询
     * @param page
     * @param pagesize
     * @param userId
     * @return
     */
    @Override
    public PageResult findLoveById(Integer page, Integer pagesize, Long userId) {
        //分页查询条件
        Query query = new Query(Criteria.where("LikeUserId").is(userId));
        query.limit(pagesize).skip((page-1)*pagesize);
        query.with(Sort.by(Sort.Order.desc("created")));
        //返回分页对象
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);
        return new PageResult(page,pagesize, (int) count,userLikeList);
    }


    @Override
    public PageResult findLikeById(List<ObjectId> objectIdList, Integer type, Integer page, Integer pagesize) {
        Query query = new Query(Criteria.where("publishId").in(objectIdList).and("commentType").is(type).
                and("pubType").is(1));
        query.limit(pagesize).skip((page-1)*pagesize);
        query.with(Sort.by(Sort.Order.desc("created")));
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        return new PageResult(page,pagesize, (int) count,commentList);
    }
}
